import { userAPI } from '@/api'
import { ElMessage } from 'element-plus'

// 后端连接测试工具
export const backendConnectionTest = {
  // 测试后端服务连接
  async testConnection() {
    console.log('🔍 测试后端连接 (端口: 8080)...')
    
    try {
      // 尝试获取用户列表作为连接测试
      const response = await userAPI.getList()
      
      console.log('✅ 后端连接成功!')
      console.log('📍 服务器地址: http://localhost:8080')
      console.log('📊 响应数据:', response)
      
      ElMessage.success('后端连接成功 - 端口 8080')
      return { 
        success: true, 
        message: '后端连接成功',
        server: 'http://localhost:8080',
        data: response 
      }
    } catch (error) {
      console.error('❌ 后端连接失败:', error)
      console.error('📍 请检查后端服务是否在端口 8080 运行')
      
      ElMessage.error('后端连接失败，请检查端口 8080 的服务状态')
      return { 
        success: false, 
        message: '后端连接失败',
        error: error.message,
        server: 'http://localhost:8080'
      }
    }
  },

  // 测试所有用户接口
  async testAllUserEndpoints() {
    console.log('🧪 测试所有用户接口 (端口: 8080)...')
    
    const results = {
      connection: await this.testConnection(),
      login: await this.testLogin(),
      create: await this.testCreate(),
      update: await this.testUpdate(),
      list: await this.testList(),
      page: await this.testPage(),
      detail: await this.testDetail(),
      delete: await this.testDelete()
    }
    
    console.log('📊 接口测试结果:', results)
    
    const successCount = Object.values(results).filter(r => r.success).length
    const totalCount = Object.keys(results).length
    
    console.log(`✅ 成功: ${successCount}/${totalCount}`)
    
    if (successCount === totalCount) {
      ElMessage.success('🎉 所有接口测试通过!')
    } else {
      ElMessage.warning(`⚠️ 部分接口测试失败 (${successCount}/${totalCount})`)
    }
    
    return results
  },

  // 测试登录接口
  async testLogin() {
    try {
      const credentials = {
        username: 'test@example.com',
        password: '123456'
      }
      const response = await userAPI.login(credentials)
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 测试创建用户接口
  async testCreate() {
    try {
      const userData = {
        username: 'testuser',
        email: 'test@example.com',
        phone: '13800138000',
        password: '123456',
        realName: '测试用户',
        userType: 'user'
      }
      const response = await userAPI.create(userData)
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 测试更新用户接口
  async testUpdate() {
    try {
      const updateData = {
        id: 1,
        username: 'updateduser',
        email: 'updated@example.com',
        phone: '13800138001',
        realName: '更新用户'
      }
      const response = await userAPI.update(updateData)
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 测试用户列表接口
  async testList() {
    try {
      const response = await userAPI.getList()
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 测试分页查询接口
  async testPage() {
    try {
      const pageData = {
        page: 1,
        size: 10,
        username: '',
        email: '',
        phone: '',
        userType: ''
      }
      const response = await userAPI.getPage(pageData)
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 测试用户详情接口
  async testDetail() {
    try {
      const response = await userAPI.getInfo(1)
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 测试删除用户接口
  async testDelete() {
    try {
      const response = await userAPI.delete(1)
      return { success: true, data: response }
    } catch (error) {
      return { success: false, error: error.message }
    }
  },

  // 显示配置信息
  showConfig() {
    console.log('📋 后端配置信息:')
    console.log('📍 服务器地址: http://localhost:8080')
    console.log('🔗 API 前缀: /api')
    console.log('🌐 代理配置: 已启用')
    console.log('🔒 Mock 状态: 已禁用')
    console.log('🐛 调试模式: 已启用')
    
    return {
      server: 'http://localhost:8080',
      apiPrefix: '/api',
      proxyEnabled: true,
      mockDisabled: true,
      debugEnabled: true
    }
  }
}

// 导出测试函数
export default backendConnectionTest 